﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AffinityORMLibrary;
using AffinityORMLibrary.DataTypes;
using AffinityORM.BusinessLayer;
using AffinityORM.DataTypes;
using System.IO;
using System.Configuration;

namespace AffinityORM
{
    public partial class AffinityORM : Form
    {
        private List<SysTable> _sysTables;
        public List<SysTable> SysTables
        {
            get { return _sysTables; }
        }

        private Project _currentProject;
        public Project CurrentProject
        {
            get { return _currentProject; }
            set { _currentProject = value; }
        }

        private List<UdlConnection> _udlConnections;
        public List<UdlConnection> UdlConnections
        {
            get { return _udlConnections; }
            set { _udlConnections = value; }
        }

        public AffinityORM()
        {
            InitializeComponent();
            LoadConnectionPicker();
        }

        public void UpdateSchema(ConnectionStringSettings connSettings)
        {
            this.SchemaColumnsInfo.DataSource = null;
            this.SchemaInfoGrid.DataSource = null;
            ModelAssembler a = new ModelAssembler(connSettings);
            _sysTables = a.GetSysTables();

            if (_sysTables.Count > 0)
            {
                this.SchemaInfoGrid.DataSource = SysTables;
                this.SchemaInfoGrid.Rows[0].Selected = true;
                this.SchemaInfoGrid.Columns["TableInfo"].Visible = false;
            }
            else
            {
                this.SchemaColumnsInfo.DataSource = null;
            }

            UpdateColumns();
        }

        public void UpdateColumns()
        {
            if (this.SchemaInfoGrid.SelectedRows.Count > 0)
            {
                this.SchemaColumnsInfo.DataSource = ((SysTable)this.SchemaInfoGrid.SelectedRows[0].DataBoundItem).Columns;
                this.SchemaColumnsInfo.Columns["OldKeys"].Visible = false;
                this.SchemaColumnsInfo.Columns["TABLE_NAME"].Visible = false;
            }
            else
            {
                this.SchemaColumnsInfo.DataSource = null;
            }
        }

        private void SchemaInfoGrid_SelectionChanged(object sender, EventArgs e)
        {
            UpdateColumns();
        }

        private void NewConnectionMenuItem_Click(object sender, EventArgs e)
        {
            Connection dialog = new Connection();
            dialog.ShowDialog();
        }

        private void connectionPicker_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateSchema(((UdlConnection)this.connectionPicker.SelectedItem).ConnectionStringSettings);
        }

        private void LoadConnectionPicker()
        {
            UdlConnectionManager manager = new UdlConnectionManager();
            UdlConnections = manager.GetUdlConnections();
            if (UdlConnections.Count > 0)
            {
                connectionPicker.DataSource = UdlConnections;
                connectionPicker.DisplayMember = "FileName";
                connectionPicker.ValueMember = "FileName";
            }
            else
            {
                connectionPicker.DataSource = null;
            }
        }

        private void UdlConnectionRefreshBTN_Click(object sender, EventArgs e)
        {
            LoadConnectionPicker();
        }
    }
}
